package gold.digger;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC797 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int n = 0;

        public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
            n = graph.length - 1;
            List<List<Integer>> res = new ArrayList<>();
            List<Integer> curPath = new LinkedList<>();
            dfs(graph, 0, curPath, res);
            return res;
        }

        public void dfs(int[][] graph, int cur, List<Integer> curPath, List<List<Integer>> res) {
            curPath.add(cur);
            if (cur == n) {
                res.add(copyPath(curPath));
                curPath.remove(curPath.size() - 1);
                return;
            }

            for (int next : graph[cur]) {
                dfs(graph, next, curPath, res);
            }
            curPath.remove(curPath.size() - 1);
        }

        public List<Integer> copyPath(List<Integer> path) {
            List<Integer> dup = new ArrayList<Integer>();
            for (Integer node : path) {
                dup.add(node);
            }
            return dup;
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[][] arr = {{4, 3, 1}, {3, 2, 4}, {3}, {4}, {}};
        System.out.println(solution.allPathsSourceTarget(arr));
    }

    public static void main(String[] args) throws Exception {
        LC797 an = new LC797();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
